<template>
  <div>
    <vxe-toolbar id="toolbarId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button @click="setMultipleRead">批量标记已读</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      height="700"
      :data="tableData"
      :loading="loading"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column type="checkbox" width="60" />
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="130"
          field="orderDetailNumber"
          title="运输单号"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.orderDetailNumber" placeholder="运输单号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="130"
          field="driverName"
          title="司机姓名"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.driverName" placeholder="司机姓名" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="130"
          field="driverPhone"
          title="手机号"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.driverPhone" placeholder="手机号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="200"
          field="deliveryFeeDeserved"
          title="应收运费"
          sortable
        />
        <template v-slot:header>
          <div class="for-input-range">
            <el-input v-model="listQuery.deliveryFeeDeservedStart" @change="isNumber4listQuery('deliveryFeeDeservedStart')" placeholder="下限" clearable />
            <el-input v-model="listQuery.deliveryFeeDeservedEnd" @change="isNumber4listQuery('deliveryFeeDeservedEnd')" placeholder="上限" clearable />
          </div>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="200"
          field="deliveryFeePractical"
          title="实收运费"
          sortable
        />
        <template v-slot:header>
          <div class="for-input-range">
            <el-input v-model="listQuery.deliveryFeePracticalStart" @change="isNumber4listQuery('deliveryFeePracticalStart')" placeholder="下限" clearable />
            <el-input v-model="listQuery.deliveryFeePracticalEnd" @change="isNumber4listQuery('deliveryFeePracticalEnd')" placeholder="上限" clearable />
          </div>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="200"
          field="deliveryOilFee"
          title="分配油卡"
          sortable
        />
        <template v-slot:header>
          <div class="for-input-range">
            <el-input v-model="listQuery.deliveryOilFeeStart" @change="isNumber4listQuery('deliveryOilFeeStart')" placeholder="下限" clearable />
            <el-input v-model="listQuery.deliveryOilFeeEnd" @change="isNumber4listQuery('deliveryOilFeeEnd')" placeholder="上限" clearable />
          </div>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="200"
          field="deliveryCashFee"
          title="实收现金"
          sortable
        />
        <template v-slot:header>
          <div class="for-input-range">
            <el-input v-model="listQuery.deliveryCashFeeStart" @change="isNumber4listQuery('deliveryCashFeeStart')" placeholder="下限" clearable />
            <el-input v-model="listQuery.deliveryCashFeeEnd" @change="isNumber4listQuery('deliveryCashFeeEnd')" placeholder="上限" clearable />
          </div>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          width="200"
          field="extraFee"
          title="其他费用"
          sortable
        />
        <template v-slot:header>
          <div class="for-input-range">
              <el-input v-model="listQuery.extraFeeStart" @change="isNumber4listQuery('extraFeeStart')" placeholder="下限" clearable />
              <el-input v-model="listQuery.extraFeeEnd" @change="isNumber4listQuery('extraFeeEnd')" placeholder="上限" clearable />
          </div>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="130"
          field="isRead"
          title="是否已读"
          :formatter="formatterCommonStatus"
          sortable
        />
        <template v-slot:header>
          <el-select v-model="listQuery.isRead" clearable placeholder="请选择">
            <el-option
              v-for="item in commonStatusList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="240"
          field="gmtCreated"
          title="创建日期"
          sortable
        />
        <template v-slot:header>
          <el-date-picker
            class="unite-data"
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeCreatedDate"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          width="240"
          field="gmtModified"
          title="更新日期"
          sortable
        />
        <template v-slot:header>
          <el-date-picker
            class="unite-data"
            v-model="gmtModifiedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeModifiedDate"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" width="50" fixed="right">
        <template slot-scope="scope">
          <el-tooltip
            v-if="scope.row.isRead === 0"
            class="item"
            effect="dark"
            content="标记已读"
            placement="top"
          >
            <vxe-button type="text" @click="setSingleRead(scope.row.code)">
              <i class="el-icon-folder" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip v-else class="item" effect="dark" content="已读" placement="top">
            <vxe-button type="text">
              <i class="el-icon-folder-opened" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, { mounts: ["cookie"] });
import {
  getPaymentRecordsList,
  updateReadStatus,
  updateCheckedReadStatus
} from "@/api/news";
import { isNumber } from '@/utils/validate.js'
export default {
  name: "PaymentRecordsList",
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      loading: false,
      tableData: [],
      gmtCreatedRange: "",
      gmtModifiedRange: "",
      listQuery: {
        orderDetailNumber: "",
        driverName: "",
        driverPhone: "",
        deliveryFeeDeserved: "",
        deliveryFeePractical: "",
        deliveryOilFee: "",
        deliveryCashFee: "",
        extraFee: "",
        isRead: "",
        gmtCreatedStart: "",
        gmtCreatedEnd: "",
        gmtModifiedStart: "",
        gmtModifiedEnd: "",
        pageNum: 1,
        pageSize: 10,
        sortField: "",
        sortType: "",
        total: 0
      },
      // 通用是否状态
      commonStatusList: [
        { label: "未读", value: 0 },
        { label: "已读", value: 1 }
      ]
    };
  },
  created() {
    this.findList();
  },
  methods: {
    isNumber4listQuery(key){
      let val = this.listQuery[key];
      if(val!==''){
        if(!isNumber(val)){
          this.$message({ message: '请输入数字！', type: 'error' })
        }
        val = parseFloat(val);
        if(isNaN(val)){
          val = '';
        }
        this.$set(this.listQuery,key,val);
      }
    },
    // 列表
    findList() {
      this.loading = true;
      return new Promise(resolve => {
        getPaymentRecordsList(this.listQuery).then(response => {
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          this.loading = false;
          resolve();
        });
      });
    },
    // 日期
    changeCreatedDate(value) {
      if (value) {
        this.listQuery.gmtCreatedStart = value[0] + " 00:00:00";
        this.listQuery.gmtCreatedEnd = value[1] + " 23:59:59";
      } else {
        this.listQuery.gmtCreatedStart = "";
        this.listQuery.gmtCreatedEnd = "";
      }
    },
    changeModifiedDate(value) {
      if (value) {
        this.listQuery.gmtModifiedStart = value[0] + " 00:00:00";
        this.listQuery.gmtModifiedEnd = value[1] + " 23:59:59";
      } else {
        this.listQuery.gmtModifiedStart = "";
        this.listQuery.gmtModifiedEnd = "";
      }
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    },
    // 翻译通用是否状态
    formatterCommonStatus({ cellValue }) {
      const item = this.commonStatusList.find(item => item.value === cellValue);
      return item ? item.label : "";
    },
    // 标记单项已读
    setSingleRead(code) {
      this.$confirm("确认标记已读吗？").then(_ => {
        updateReadStatus({ isRead: 1, code }).then(response => {
          this.$message({ message: response.msg, type: "success" });
          this.findList();
        });
      });
    },
    // 标记多项已读
    setMultipleRead() {
      const checkedRowArray = this.$refs.xTable.getSelectRecords();
      if (!checkedRowArray || checkedRowArray.length === 0) {
        this.$message({ message: "请选择需要设置的项目", type: "warning" });
        return false;
      }
      const isReadArray = checkedRowArray.filter(item => item.isRead === 1);
      if (isReadArray.length) {
        this.$message({ message: "已读消息请勿重复标记", type: "warning" });
        return false;
      }
      const codes = checkedRowArray.map(item => item.code).join(",");
      updateCheckedReadStatus({ isRead: 1, codes }).then(response => {
        this.$message({ message: response.msg, type: "success" });
        this.findList();
      });
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    }
  }
};
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis),
.vxe-table .vxe-footer--column:not(.col--ellipsis),
.vxe-table .vxe-header--column:not(.col--ellipsis) {
  padding: 6px 0;
}
.vxe-toolbar {
  padding: 0 0 10px 0;
}
</style>
